package com.moxiao.yfjh.module.maker.service.matchmakerInfo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoCreateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoExportReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoPageReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoUpdateReqVO;
import com.moxiao.yfjh.module.maker.convert.matchmakerInfo.MatchmakerInfoConvert;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchmakerInfo.MatchmakerInfoDO;
import com.moxiao.yfjh.module.maker.dal.mysql.matchmakerInfo.MatchmakerInfoMapper;
import com.moxiao.yfjh.module.system.controller.admin.user.vo.user.UserCreateReqVO;
import com.moxiao.yfjh.module.system.service.dept.DeptService;
import com.moxiao.yfjh.module.system.service.permission.PermissionService;
import com.moxiao.yfjh.module.system.service.user.AdminUserService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.maker.enums.ErrorCodeConstants.MATCHMAKER_INFO_NOT_EXISTS;

/**
 * 相亲红娘信息 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MatchmakerInfoServiceImpl implements MatchmakerInfoService {

    @Resource
    private MatchmakerInfoMapper matchmakerInfoMapper;

    @Resource
    private AdminUserService userService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private DeptService deptService;

    @Override
    public Long createMatchmakerInfo(MatchmakerInfoCreateReqVO createReqVO) {
        // 为红娘创建用户
        UserCreateReqVO userCreateReqVO = new UserCreateReqVO();
        userCreateReqVO.setUsername(createReqVO.getPhone());
        userCreateReqVO.setMobile(createReqVO.getPhone());
        userCreateReqVO.setNickname(createReqVO.getName());
        userCreateReqVO.setRemark("我是红娘：" + createReqVO.getName() + "，默认密码为【123456】请及时修改密码！");
        userCreateReqVO.setPassword("123456");
        userCreateReqVO.setSex(1);
        userCreateReqVO.setUserType(3);
        userCreateReqVO.setDeptId(deptService.getDeptOne());
        userCreateReqVO.setAvatar(createReqVO.getMerchantCover());
        Long userId = userService.createUser(userCreateReqVO);
        // 赋予红娘权限-默认权限142
        Set<Long> roleIds = new HashSet<>();
        roleIds.add(142L);
        permissionService.assignUserRole(userId, roleIds);
        createReqVO.setUserId(userId);
        // 插入
        MatchmakerInfoDO matchmakerInfo = MatchmakerInfoConvert.INSTANCE.convert(createReqVO);
        matchmakerInfoMapper.insert(matchmakerInfo);
        // 返回
        return matchmakerInfo.getId();
    }

    @Override
    public void updateMatchmakerInfo(MatchmakerInfoUpdateReqVO updateReqVO) {
        // 校验存在
        validateMatchmakerInfoExists(updateReqVO.getId());
        // 更新
        MatchmakerInfoDO updateObj = MatchmakerInfoConvert.INSTANCE.convert(updateReqVO);
        matchmakerInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteMatchmakerInfo(Long id) {
        // 校验存在
        validateMatchmakerInfoExists(id);
        // 删除
        matchmakerInfoMapper.deleteById(id);
    }

    private void validateMatchmakerInfoExists(Long id) {
        if (matchmakerInfoMapper.selectById(id) == null) {
            throw exception(MATCHMAKER_INFO_NOT_EXISTS);
        }
    }

    @Override
    public MatchmakerInfoDO getMatchmakerInfo(Long id) {
        return matchmakerInfoMapper.selectById(id);
    }

    @Override
    public List<MatchmakerInfoDO> getMatchmakerInfoList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return matchmakerInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MatchmakerInfoDO> getMatchmakerInfoPage(MatchmakerInfoPageReqVO pageReqVO) {
        return matchmakerInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MatchmakerInfoDO> getMatchmakerInfoList(MatchmakerInfoExportReqVO exportReqVO) {
        return matchmakerInfoMapper.selectList(exportReqVO);
    }

    @Override
    public MatchmakerInfoDO getLastNo(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<MatchmakerInfoDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(MatchmakerInfoDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(MatchmakerInfoDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return matchmakerInfoMapper.selectOne(lqw);
    }

    @Override
    public Integer repeatChecking(String code) {
        LambdaQueryWrapper<MatchmakerInfoDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(true, MatchmakerInfoDO::getNo, code);
        List<MatchmakerInfoDO> list = matchmakerInfoMapper.selectList(lqw);
        return list.size();
    }
}
